package com.caifu.controller.drugs;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.caifu.bean.Result;
import com.caifu.controller.AbstractController;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlMeicineDistribution;
import com.caifu.service.YlMeicineDistributionService;
import com.caifu.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 药物发放记录表
 *
 * @author tww
 * ClassName: YlMeicineDistributionController.java、
 * createTime: 2020年11月12日 11:10:37
 **/
@Controller
@RequestMapping("meicinedistribution")
public class YlMeicineDistributionController extends AbstractController {

    private static final Logger logger = LoggerFactory.getLogger(YlMeicineDistributionController.class);

    @Autowired
    private YlMeicineDistributionService ylMeicineDistributionService;

    private String prefix = "medicine/";

    /**
     * 添加药物
     */
    @PostMapping("addMeicineDistribution")
    @ResponseBody
    public Result addMeicineDistribution(YlMeicineDistribution ylMeicineDistribution) {
        SysUser user = getUser();
        try {
            return ylMeicineDistributionService.addMeicineDistribution(ylMeicineDistribution,user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id查询发放药物记录
     */
    @GetMapping("getMeicineDistributionById")
    @ResponseBody
    public Result getMeicineDistributionById(String id) {
        try {
            YlMeicineDistribution ylMeicineDistribution = ylMeicineDistributionService.getMeicineDistributionById(id);
            return success(200, "success", ylMeicineDistribution);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据老人id查询老人方法药物记录
     */
    @GetMapping("getgetMeicineDistributionByIdByUserNo")
    @ResponseBody
    public Result getgetMeicineDistributionByIdByUserNo(String userNo, long currentPage, long pageSize) {
        try {
            IPage<YlMeicineDistribution> ylMeicineDistributionIPage = ylMeicineDistributionService.getgetMeicineDistributionByIdByUserNo(userNo, currentPage, pageSize);
            return success(0, "success", ylMeicineDistributionIPage);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id删除发放药物
     */
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(String id) {
        try {
            ylMeicineDistributionService.deleteById(id);
            return success(200, "删除成功");
        } catch (Exception e) {
            String msg = "网络异常";
            if (Util.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 跳药物发放、登记列表
     */
    @GetMapping("toAddMeicineDistribution")
    public String toAddMeicineDistribution(String userNo, Model model) {
        SysUser user = getUser();
        model.addAttribute("userNo", userNo);
        return prefix + "grant_drugs";
    }

    /**
     * 跳药物发放详情页
     */
    @GetMapping("toMeicineDetails")
    public String toMeicineDtials(String id, Model model) {
        model.addAttribute("id", id);
        return prefix + "grant_details";
    }

}
